package gui;

import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

public class TourneyManView {

  private Shell shell = null;// @jve:decl-index=0:visual-constraint="41,34"
  private Composite cmpMatchRounds = null;
  private Composite cmpSimulTrn = null;
  private Composite cmpMain = null;
  private ScrolledComposite cmpScroll = null;
  private Group cmpTrnSettings = null;
  public ProgressBar pbTrn = null;
  public List lstPreconfEngines = null;
  public List lstTrnEngines = null;
  public Combo chcTimeControlType = null;
  public Combo chcMainSimulEnginge = null;
  public Spinner spnMovesPerSession = null;
  public Spinner spnTimeIncrement = null;
  public Spinner spnMatchRounds = null;
  public Spinner spnTimeControl = null;
  public Spinner spnSimulCycles = null;
  public Text txtOutPGNFile = null;
  public Button btnBrowseOutPGNFile = null;
  public Button btnAddToTournament = null;
  public Button btnAddEngine = null;
  public Button btnRemoveFromTrn;
  public Button btnRunTrn;
  public Button btnLoad = null;
  public Button btnSaveTrn = null;
  public Button btnSaveTrnAs = null;
  public TabFolder tabFolder = null;
  public ListViewer preConfEnginesViewer;
  public ListViewer trnEnginesViewer;
  public ComboViewer simulViewer;
  private EngineSettings engineSettings;

  public TourneyManView() {
    createSShell();
  }

  public Shell getShell() {
    return shell;
  }

  private void createSShell() {
    FillLayout fillLayout = new FillLayout();
    fillLayout.type = SWT.VERTICAL;
    shell = new Shell();
    shell.setText("TourneyMan");
    shell.setLayout(fillLayout);
    shell.setSize(new Point(864, 626));
    createCmpScroll();
    shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
  }

  private void createCmpScroll() {
    cmpScroll = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
    createCmpMain();
    cmpScroll.setContent(cmpMain);
    cmpScroll.setExpandHorizontal(true);
    cmpScroll.setExpandVertical(true);
  }

  private void createCmpMain() {
    cmpMain = new Composite(cmpScroll, SWT.NONE);
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;
    cmpMain.setLayout(gridLayout);
    GridData gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    gridData.verticalAlignment = GridData.CENTER;
    cmpMain.setLayoutData(gridData);
    createCmpPreconfEngines();
    engineSettings = new EngineSettings();
    engineSettings.addGroupTo(cmpMain);
    createCmpTrnEngines();
    createCmpTrnSettings();
  }

  private void createCmpPreconfEngines() {
    GridData gridData13 = new GridData();
    gridData13.horizontalAlignment = GridData.FILL;
    gridData13.verticalAlignment = GridData.CENTER;
    GridData gridData10 = new GridData();
    gridData10.horizontalAlignment = GridData.FILL;
    gridData10.grabExcessHorizontalSpace = false;
    gridData10.verticalAlignment = GridData.CENTER;
    GridData gridData8 = new GridData();
    gridData8.horizontalAlignment = GridData.FILL;
    gridData8.grabExcessVerticalSpace = true;
    gridData8.grabExcessHorizontalSpace = true;
    gridData8.verticalAlignment = GridData.FILL;
    GridData gridData7 = new GridData();
    gridData7.horizontalAlignment = GridData.FILL;
    gridData7.grabExcessVerticalSpace = true;
    gridData7.grabExcessHorizontalSpace = false;
    gridData7.verticalAlignment = GridData.FILL;
    Composite cmpPreconfEngines = new Composite(cmpMain, SWT.NONE);
    cmpPreconfEngines.setLayout(new GridLayout());
    cmpPreconfEngines.setLayoutData(gridData7);
    btnAddEngine = new Button(cmpPreconfEngines, SWT.NONE);
    btnAddEngine.setText("Add engine");
    btnAddEngine.setLayoutData(gridData10);
    lstPreconfEngines = new List(cmpPreconfEngines, SWT.BORDER
                                                    | SWT.V_SCROLL);
    lstPreconfEngines.setToolTipText("List of preconfigured engines");
    lstPreconfEngines.setLayoutData(gridData8);
    btnAddToTournament = new Button(cmpPreconfEngines, SWT.NONE);
    btnAddToTournament.setText("Add to tournament");
    btnAddToTournament.setLayoutData(gridData13);
  }

  private void createCmpTrnEngines() {
    GridData gridData9 = new GridData();
    gridData9.horizontalAlignment = GridData.CENTER;
    gridData9.grabExcessVerticalSpace = true;
    gridData9.heightHint = -1;
    gridData9.widthHint = -1;
    gridData9.verticalAlignment = GridData.FILL;
    GridLayout gridLayout = new GridLayout();
    gridLayout.marginWidth = 0;
    GridData gridDataButtons = new GridData();
    gridDataButtons.horizontalAlignment = GridData.FILL;
    gridDataButtons.verticalAlignment = GridData.CENTER;
    GridData gridDataLists = new GridData();
    gridDataLists.grabExcessVerticalSpace = true;
    gridDataLists.horizontalAlignment = GridData.FILL;
    gridDataLists.verticalAlignment = GridData.FILL;
    gridDataLists.grabExcessHorizontalSpace = true;
    Composite cmpTrnEngines = new Composite(cmpMain, SWT.NONE);
    cmpTrnEngines.setLayout(gridLayout);
    cmpTrnEngines.setLayoutData(gridData9);
    lstTrnEngines = new List(cmpTrnEngines, SWT.BORDER | SWT.V_SCROLL);
    lstTrnEngines.setToolTipText("List of tournament engines");
    lstTrnEngines.setLayoutData(gridDataLists);
    btnRemoveFromTrn = new Button(cmpTrnEngines, SWT.NONE);
    btnRemoveFromTrn.setText("Remove from tournament");
  }

  private void createCmpTrnSettings() {
    GridData gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = true;
    gridData.verticalAlignment = GridData.FILL;
    cmpTrnSettings = new Group(cmpMain, SWT.NONE);
    cmpTrnSettings.setText("Tournament settings");
    cmpTrnSettings.setLayout(new GridLayout());
    cmpTrnSettings.setLayoutData(gridData);
    createTabFolder();
    createCmpOutPGNFile();
    createCmpTrnParams();
    createCmpProgressBar();
    createCmpTrnActions();
  }

  private void createCmpTrnParams() {
    GridData gridData = new GridData();
    gridData.grabExcessHorizontalSpace = true;
    gridData.verticalAlignment = GridData.CENTER;
    gridData.horizontalAlignment = GridData.FILL;
    Group cmpTrnParams = new Group(cmpTrnSettings, SWT.NONE);
    cmpTrnParams.setText("Time control settings");
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;
    cmpTrnParams.setLayout(gridLayout);
    cmpTrnParams.setLayoutData(gridData);
    Label lblTimeControl = new Label(cmpTrnParams, SWT.NONE);
    lblTimeControl.setText("Time control in minuts");
    spnTimeControl = new Spinner(cmpTrnParams, SWT.BORDER);
    spnTimeControl.setMinimum(1);
    spnTimeControl.setSelection(5);
    spnTimeControl.setMaximum(180);
    Label lblTimeControlType = new Label(cmpTrnParams, SWT.NONE);
    lblTimeControlType.setText("Time control type");
    chcTimeControlType = new Combo(cmpTrnParams, SWT.NONE);
    Label lblTimeIncrement = new Label(cmpTrnParams, SWT.NONE);
    lblTimeIncrement.setText("Time increment in seconds");
    spnTimeIncrement = new Spinner(cmpTrnParams, SWT.BORDER);
    spnTimeIncrement.setMinimum(1);
    spnTimeIncrement.setSelection(10);
    Label lblMovesPerSession = new Label(cmpTrnParams, SWT.NONE);
    lblMovesPerSession.setText("Moves per session");
    spnMovesPerSession = new Spinner(cmpTrnParams, SWT.BORDER);
    spnMovesPerSession.setMinimum(1);
    spnMovesPerSession.setSelection(40);
  }

  private void createCmpTrnActions() {
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 4;
    gridLayout.horizontalSpacing = 5;
    gridLayout.verticalSpacing = 5;
    gridLayout.marginHeight = 5;
    GridData gridDataBtn = new GridData();
    gridDataBtn.grabExcessHorizontalSpace = true;
    gridDataBtn.verticalAlignment = GridData.CENTER;
    gridDataBtn.horizontalAlignment = GridData.FILL;
    GridData gridDataCmp = new GridData();
    gridDataCmp.horizontalAlignment = GridData.FILL;
    gridDataCmp.grabExcessHorizontalSpace = true;
    gridDataCmp.grabExcessVerticalSpace = true;
    gridDataCmp.verticalAlignment = GridData.END;
    Composite cmpTrnActions = new Composite(cmpTrnSettings, SWT.NONE);
    cmpTrnActions.setLayoutData(gridDataCmp);
    cmpTrnActions.setLayout(gridLayout);
    btnSaveTrn = new Button(cmpTrnActions, SWT.NONE);
    btnSaveTrn.setText("Save");
    btnSaveTrn.setLayoutData(gridDataBtn);
    btnSaveTrnAs = new Button(cmpTrnActions, SWT.NONE);
    btnSaveTrnAs.setText("Save as");
    btnSaveTrnAs.setLayoutData(gridDataBtn);
    btnLoad = new Button(cmpTrnActions, SWT.NONE);
    btnLoad.setText("Load");
    btnLoad.setLayoutData(gridDataBtn);
    btnRunTrn = new Button(cmpTrnActions, SWT.NONE);
    btnRunTrn.setText("Run ");
    btnRunTrn.setLayoutData(gridDataBtn);
  }

  private void createCmpProgressBar() {
    GridData gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.verticalAlignment = GridData.CENTER;
    Composite cmpProgressBar = new Composite(cmpTrnSettings, SWT.NONE);
    cmpProgressBar.setLayout(new FillLayout());
    cmpProgressBar.setLayoutData(gridData);
    pbTrn = new ProgressBar(cmpProgressBar, SWT.HORIZONTAL | SWT.SMOOTH);
  }

  private void createCmpOutPGNFile() {
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    gridLayout.marginHeight = 0;
    gridLayout.verticalSpacing = 5;
    gridLayout.marginWidth = 5;
    GridData gridDataTxt = new GridData();
    gridDataTxt.horizontalAlignment = GridData.FILL;
    gridDataTxt.grabExcessHorizontalSpace = true;
    gridDataTxt.verticalAlignment = GridData.CENTER;
    GridData gridDataCmp = new GridData();
    gridDataCmp.horizontalAlignment = GridData.FILL;
    gridDataCmp.grabExcessHorizontalSpace = true;
    gridDataCmp.grabExcessVerticalSpace = false;
    gridDataCmp.verticalAlignment = GridData.BEGINNING;
    Composite cmpOutPGNFile = new Composite(cmpTrnSettings, SWT.NONE);
    cmpOutPGNFile.setLayoutData(gridDataCmp);
    cmpOutPGNFile.setLayout(gridLayout);
    Label lblOutPgnFile = new Label(cmpOutPGNFile, SWT.NONE);
    lblOutPgnFile.setText("Out PGN file");
    txtOutPGNFile = new Text(cmpOutPGNFile, SWT.BORDER);
    txtOutPGNFile.setLayoutData(gridDataTxt);
    btnBrowseOutPGNFile = new Button(cmpOutPGNFile, SWT.NONE);
    btnBrowseOutPGNFile.setText("...");
  }

  private void createCmpMatchRounds() {
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;
    cmpMatchRounds = new Composite(tabFolder, SWT.NONE);
    cmpMatchRounds.setLayout(gridLayout);
    GridData gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    gridData.verticalAlignment = GridData.CENTER;
    cmpMatchRounds.setLayoutData(gridData);
    Label lblMatchRounds = new Label(cmpMatchRounds, SWT.NONE);
    lblMatchRounds.setText("Match rounds");
    spnMatchRounds = new Spinner(cmpMatchRounds, SWT.BORDER);
  }

  private void createCmpSimulTrn() {
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;
    cmpSimulTrn = new Composite(tabFolder, SWT.NONE);
    cmpSimulTrn.setLayout(gridLayout);
    GridData gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    gridData.verticalAlignment = GridData.CENTER;
    cmpSimulTrn.setLayoutData(gridData);
    Label lblMainEngine = new Label(cmpSimulTrn, SWT.NONE);
    lblMainEngine.setText("Select main engine");
    chcMainSimulEnginge = new Combo(cmpSimulTrn, SWT.NONE);
    chcMainSimulEnginge.setLayoutData(gridData);
    Label lblNumberOfCycles = new Label(cmpSimulTrn, SWT.NONE);
    lblNumberOfCycles.setText("Number of cycles");
    spnSimulCycles = new Spinner(cmpSimulTrn, SWT.BORDER);
    spnSimulCycles.setMinimum(1);
  }

  private void createTabFolder() {
    GridData gridData = new GridData();
    gridData.grabExcessHorizontalSpace = true;
    gridData.verticalAlignment = GridData.CENTER;
    gridData.horizontalAlignment = GridData.FILL;
    tabFolder = new TabFolder(cmpTrnSettings, SWT.NONE);
    tabFolder.setLayoutData(gridData);
    TabItem one = new TabItem(tabFolder, SWT.NONE);
    one.setText("Round robin");
    createCmpMatchRounds();
    one.setControl(cmpMatchRounds);
    TabItem two = new TabItem(tabFolder, SWT.NONE);
    two.setText("Simul");
    createCmpSimulTrn();
    two.setControl(cmpSimulTrn);
  }

  public EngineSettings getEngineSettings() {
    return engineSettings;
  }
}
